package com.github.cyf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.cyf.common.module.response.Rs;
import com.github.cyf.domain.po.User;
import com.github.cyf.service.PermissionService;
import com.github.cyf.service.RoleService;
import com.github.cyf.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author chenyifan
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RoleService roleService;

    @PostMapping("/addOrUpdateUser")
    @ApiOperation("1、添加或修改用户信息")
    @ApiOperationSupport(order = 1)
    @SaCheckPermission(value = {"admin:user:insert", "admin:user:update"})
    public Rs addOrUpdateUser(@RequestBody @Validated User user){
        return Rs.ok().setData(userService.saveOrUpdate(user));
    }

    @GetMapping("/{id}")
    @ApiOperation("2、查询用户信息")
    @ApiOperationSupport(order = 2)
    @SaCheckPermission(value = "admin:user:select")
    public Rs getUser(@PathVariable Long id){
        return Rs.ok().setData(userService.getById(id));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("3、删除用户")
    @ApiOperationSupport(order = 3)
    @SaCheckPermission(value = "admin:user:delete")
    public Rs deleteUser(@PathVariable Long id){
        return Rs.ok().setData(userService.removeById(id));
    }

    @GetMapping("/getUserRole/{id}")
    @ApiOperation("4、查询用户角色信息")
    @ApiOperationSupport(order = 4)
    @SaCheckPermission(value = "admin:user:select")
    public Rs getUserRole(@PathVariable Long id){
        return Rs.ok().setData(roleService.queryRoleByUserId(id));
    }

    @GetMapping("/selectAll")
    @ApiOperation("5、查询角色列表")
    @ApiOperationSupport(order = 5)
    @SaCheckPermission(value = "admin:role:select")
    public Rs selectRole(){
        return Rs.ok().setData(roleService.list());
    }

}
